import './resource.less'
export default {
    data() {
        return {
            form: {
                edit: 1,   // 0：添加  1：修改
                ref: 'resourcerForm',
                rules: {
                    name: { required: true, message: '请输入名称', trigger: 'blur' },
                },
                children: false
            },
            parentId: 0,
            parentName: '',
            formData: {
                name: '',
                type: 0,
                url: '',
                router: '',
                permissionKey: '',
                icon: ''
            },
            defaultProps: {
                label: 'name',
                children: 'childs'
            },
            allResource: []
        }
    },
    mounted() {
        this.$store.dispatch('setBreadData', [
            {
                name: '系统管理'
            },
            {
                name: '资源管理'
            }
        ])
        this.queryAllResource()
    },

    methods: {
        // 添加资源
        addResource() {
            this.resetForm()
            this.form.edit = 0

            this.parentId = 0
            this.parentName = ''
        },
        // 添加子资源
        addChildResource() {
            if (!this.formData.id) {
                this.$msg.error('请选择父级资源')
                return
            }
            this.parentId = this.formData.id
            this.parentName = this.formData.name

            this.form.edit = 0

            this.resetForm()
        },
        // 查询资源列表
        queryAllResource() {
            api.queryAllResource().then(res => {
                this.allResource = []
                this.allResource = res.tree
            }).catch(res => {
                console.log(res)
            })
        },
        // 节点被点击时的回调	
        nodeClick(data) {
            this.resetForm()
            this.form.children = !!data.childs && !!data.childs.length

            this.parentName = ''

            this.formData.id = data.id
            this.formData.name = data.name
            // this.formData.parentId = data.parentId
            this.formData.type = data.type
            this.formData.url = data.url
            this.formData.router = data.router
            this.formData.permissionKey = data.permissionKey
            this.formData.icon = data.icon
        },
        // 保存
        sumbitForm() {
            this.$refs[this.form.ref].validate((valid) => {
                if (valid) {
                    let _parma = {
                        type: this.form.edit,
                        data: {
                            name: this.formData.name,
                            type: this.formData.type,
                            url: this.formData.url,
                            router: this.formData.router,
                            permissionKey: this.formData.permissionKey,
                            icon: this.formData.icon
                        }
                    }
                    if (this.form.edit == 0) {
                        // 添加子资源
                        if (this.parentId) {
                            _parma.data.parentId = this.parentId
                        }
                    }
                    else if (this.form.edit == 1) {
                        _parma.data.id = this.formData.id
                    }
                    api.editResource(_parma).then(res => {
                        if (this.form.edit == 0) {
                            this.$msg.success('添加成功')
                        }
                        else {
                            this.$msg.success('修改成功')
                        }

                        this.queryAllResource()
                        this.resetForm()

                        this.parentId = 0
                        this.parentName = ''
                    }).catch(res => {
                        console.log(res)
                    })
                } else {
                    this.$msg.error('验证失败')
                    return false
                }
            })

        },
        // 删除资源
        deleteResource() {
            if (!this.formData.id) return
            if (this.form.children) {
                this.$msg.error('该资源下有子资源,不能删除')
                return false
            }
            api.deleteResource({ id: this.formData.id }).then(res => {
                this.$msg.success('删除成功')
                this.queryAllResource()
                this.resetForm()

                this.parentId = 0
                this.parentName = ''
            }).catch(res => {
                console.log(res)
            })
        },
        // 重置form值
        resetForm() {
            this.form.children = false

            this.formData.name = ''
            this.formData.url = ''
            this.formData.router = ''
            this.formData.permissionKey = ''
            this.formData.icon = ''
        }
    }
}